const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const path = require('path')

// 设置静态资源目录
app.use('/public', express.static(path.join(__dirname, './static')));

// 数据库连接
require('./model/connect')

// 处理参数
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));

// 设置允许跨域访问该服务
app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', 'X-Requested-With, mytoken')
  res.header('Access-Control-Allow-Headers', 'X-Requested-With, Authorization')
  // res.setHeader('Content-Type', 'application/json;charset=utf-8')
  res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With')
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
  // res.header('X-Powered-By', ' 3.2.1')
  next();
});

// 中间件 token验证
app.use('/', require('./middleware/tokenValidate'));


// 路由
require('./route')(app);

app.listen(80)
console.log('服务器启动成功')